package EA.testproblems;
import EA.*;

/**
<table border="0" cellpadding="2" cellspacing="0">
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Problem description</b></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top" width="200"><b>Name:</b></td>
  <td valign="top">Rastrigin F1</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Nickname:</b></td>
  <td valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Intended usage:</b></td>
  <td valign="top">Test of multimodal on problems with extremely many peaks.</td>
</tr>

<tr>
  <td colspan="2" valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Problem details</b></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Function:</b></td>
  <td valign="top">x*x - 10*cos(2*pi*x) + 10 + y*y - 10*cos(2*pi*y) + 10</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Plots:</b></td>
  <td valign="top"><img src="../../images/testproblems/rastriginf1.gif">&nbsp;&nbsp;
<img src="../../images/testproblems/rastriginf1_contour.gif"></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Ranges:</b></td>
  <td valign="top">x = [-10:3.0]&nbsp;&nbsp;y = [-3.0:10.0] </td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Type:</b></td>
  <td valign="top">Minimization</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>No. of maximas:</b></td>
  <td valign="top">More than 50</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>No. of minimas:</b></td>
  <td valign="top">More than 50</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Optima radius:</b></td>
  <td valign="top">0.2</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Optima descriptions:</b></td>
  <td valign="top">The minimas are located near (0,0)</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Known optimas:</b></td>
  <td valign="top">
GMIN(0, 0)
LMIN(0, &plusmn;0.9949586375),
LMIN(&plusmn;0.9949586375, 0),
LMIN(0, &plusmn;1.989912233),
LMIN(&plusmn;1.989912233, 0),
LMIN(&plusmn;0.9949586375, &plusmn;0.9949586375),
LMIN(&plusmn;1.989912233, &plusmn;0.9949586375),
LMIN(&plusmn;0.9949586375, &plusmn;1.989912233),
LMIN(&plusmn;1.989912233, &plusmn;1.989912233),

<br><font size=1>Capital letters 
means that the precise optima is known, lowercase letters is the best known 
so far.</font></td>
</tr>
<tr>
  <td colspan="2" valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Plotting details</b></td>
</tr>

<tr bgcolor="#e0e0e0">
  <td valign="top"><b>GNUPlot code:</b></td>
  <td valign="top">
  set hidden3d<br>
  set isosamples 70<br>
  set view 60,20<br>
  splot [-10:3] [-3:10] x*x - 10*cos(2*pi*x) + 10 + y*y - 10*cos(2*pi*y) + 10<br>
</td>
</tr>

</table>

*/
public class RastriginF1 extends NumericalProblem
{
  // Easier way to build max
  private double[][] lmax =  {{1.507640732, -1.507640732},
			      {1.507640732, 1.507640732},
			      {-1.507640732, 1.507640732},
			      {-1.507640732, -1.507640732},

			      {-.5025460365, .5025460365},
			      {.5025460365, -.5025460365},
			      {.5025460365, .5025460365},
			      {-.5025460365, -.5025460365},

			      {1.507640732, -.5025460365},
			      {1.507640732, .5025460365},
			      {-1.507640732, -.5025460365},
			      {-1.507640732, .5025460365},

			      {-.5025460365, -1.507640732},
			      {-.5025460365, 1.507640732},
			      {.5025460365, -1.507640732},
			      {.5025460365, 1.507640732}};

  private double[][] lmin =  {{.9949586375, -.9949586375},
			      {.9949586375, .9949586375},
			      {-.9949586375, .9949586375},
			      {-.9949586375, -.9949586375},

			      {.9949586375, -1.989912233},
			      {-.9949586375, -1.989912233},
			      {-.9949586375, 1.989912233},
			      {.9949586375, 1.989912233},

			      {1.989912233, -1.989912233},
			      {-1.989912233, 1.989912233},
			      {-1.989912233, -1.989912233},
			      {1.989912233, 1.989912233},

			      {-1.989912233, .9949586375},
			      {-1.989912233, -.9949586375},
			      {1.989912233, .9949586375},
			      {1.989912233, -.9949586375},

			      {0, -1.989912233},
			      {0, 1.989912233},
			      {1.989912233, 0},
			      {-1.989912233, 0},

			      {0, .9949586375},
			      {0, -.9949586375},
			      {.9949586375, 0},
			      {-.9949586375, 0},

			      {0, 0}};

  public RastriginF1()
    {
      super();

      double[] optimas;

      name = "Rastrigin F1";
      objectivefunction = new NumericalFitness(){
	      public double Fitness_calcFitness_inner(double[] realpos)
	      {
		  return realpos[0]*realpos[0] - 10*Math.cos(2*Math.PI*realpos[0]) + 10 + realpos[1]*realpos[1] - 10*Math.cos(2*Math.PI*realpos[1]) + 10;
	      };
	  };
      dimensions = 2;
      ismaximization = false;
      optimumradius = 0.2;

      intervals = new Interval[2];
      intervals[0] = new Interval(-10,3);
      intervals[1] = new Interval(-3,10);

      // Set up known maximas
      knownmaxima = new NumericalOptimum[lmax.length];

      for (int i=0;i<lmax.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmax[i][0];
	optimas[1] = lmax[i][1];
	knownmaxima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), true, false, i);
      }

      // Set up known minimas
      knownminima = new NumericalOptimum[lmin.length];

      for (int i=0;i<lmin.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmin[i][0];
	optimas[1] = lmin[i][1];
	knownminima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), false, false, i);
      }
    }
}



